package com.example.mediaplayerdemo.chart.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;

import com.example.mediaplayerdemo.R;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;

import java.util.ArrayList;
import java.util.List;

public class BarChartActivity extends AppCompatActivity {
    BarChart barChart;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bar_chart);
        barChart = findViewById(R.id.BarChart);

        XAxis xAxis = barChart.getXAxis();
        LimitLine xLimitLine = new LimitLine(1.5f, "LimitLine");
        xLimitLine.setLineColor(Color.BLUE);
        xLimitLine.setTextColor(Color.BLUE);
        xAxis.addLimitLine(xLimitLine);
        xAxis.setDrawLimitLinesBehindData(true);

        List<BarEntry> entries = new ArrayList<>();
        entries.add(new BarEntry(0f, 30f));
        entries.add(new BarEntry(1f, 80f));
        entries.add(new BarEntry(2f, 60f));
        entries.add(new BarEntry(3f, 50f));
//        2 gap
        entries.add(new BarEntry(5f, 70f));
        entries.add(new BarEntry(6f, 60f));

        List<BarEntry> entries2 = new ArrayList<>();
        entries2.add(new BarEntry(0f, 24f));
        entries2.add(new BarEntry(1f, 35f));
        entries2.add(new BarEntry(2f, 84f));
        entries2.add(new BarEntry(3f, 25f));
//        2 gap
        entries2.add(new BarEntry(5f, 76f));
        entries2.add(new BarEntry(6f, 34f));
        BarDataSet set = new BarDataSet(entries, "BarDataSet 1");
        BarDataSet set2 = new BarDataSet(entries2, "BarDataSet 2");
        set2.setColor(Color.RED);
        BarData data = new BarData(set, set2);
        data.setBarWidth(0.45f);
        data.groupBars(0f, 0.06f, 0.02f);
        barChart.setData(data);
        barChart.setFitBars(true);
        barChart.invalidate();
    }
}